<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue</title>
    <script src="./vue.js"></script>
    <style>
        #root {
            width: 500px;
            margin: 200px auto;
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="root">
        <input type="text" placeholder="search" v-model='keyword'>
        <button @click='sortType=1'>升序</button>
        <button @click='sortType=2'>降序</button>
        <button @click='sortType=0'>原顺序</button>
        <ul>
            <li v-for='gua,index in cache' :key="gua.id">{{gua.name}}--{{gua.price}}--{{gua.id}}</li>
        </ul>
    </div>
    <script>
        Vue.config.productionTip = false
        const vm = new Vue({
            el: '#root',
            data: {
                keyword: '',
                guas: [{
                    name: '金瓜粒子',
                    price: '两块钱一斤',
                    id: '001'
                }, {
                    name: '金瓜皮',
                    price: '10块钱一斤',
                    id: '002'
                }, {
                    name: '银瓜皮',
                    price: '5块钱一斤',
                    id: '003'
                }, {
                    name: '铁瓜皮',
                    price: '1块钱一斤',
                    id: '004'
                }],
                sortType: 0 //1升2降
            },
            computed: {
                cache() {
                    var arr = this.guas.filter((r) => {
                        return r.name.indexOf(this.keyword) !== -1
                    })
                    if (this.sortType) {
                        arr.sort((a, b) => {
                            return this.sortType === 1 ? a.id - b.id : b.id - a.id
                        })
                    }
                    // sort函数，return中a-b为升序，b-a为降序
                    return arr
                        // 计算属性一定要加的return作为属性的值
                }
            },
            watch: {},
            methods: {},
        })
    </script>
</body>

</html>